<template>
  <div class="member-input">
    <img v-if="!member" class="qrcode" :src="this.qrcode">
    <img v-else class="qrcode" :src="member.avatar">
    <p class="nickname">
      <template v-if="!member">微信扫一扫</template>
      <template v-else>{{ member.nickname }}</template>
    </p>

    <!--S二维码过期提示-->
    <div
      v-if="qrcode_past && !member"
      id="qrcode-past"
      @click="getQrcode"
    >
      <div class="qrcode-past">
        <i class="el-icon-refresh-right" style="font-size: 40px;"></i>
        <p>二维码过期</p>
      </div>
    </div>
    <!--E二维码过期提示-->

    <!--S关闭-->
    <div v-if="member" id="close" @click="getQrcode">
      <i class="el-icon-close" style="font-size: 25px;"></i>
    </div>
    <!--E关闭-->

  </div>
</template>

<script>
// 扫码获取前端用户信息
import QRCode from 'qrcode'
let ScanTimer = null
export default {
  name: 'MemberInput',
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    // 会员id
    value: {
      type: Number,
      default: 0
    },
    // 状态改变
    toggle: {
      type: Number,
      default: 0
    }
  },
  watch: {
    toggle(value) {
      if (value > 0 && !this.value) {
        this.getQrcode()
      } else if (value > 0 && this.value) {
        this.getMember()
      } else if (value < 0) {
        this.member = null
        clearInterval(ScanTimer)
      }
    }
  },
  data() {
    return {
      // 用户信息
      member: null,
      // 二维码
      qrcode: '',
      // 二维码是否过期
      qrcode_past: false
    }
  },
  mounted() {
    if (this.value) {
      this.getMember()
    } else {
      this.getQrcode()
    }
    clearInterval(ScanTimer)
  },
  destroyed() {
    clearInterval(ScanTimer)
  },
  methods: {
    /**
     * 创建二维码
     */
    getQrcode() {
      this.qrcode_past = false
      this.member = null
      this.$get('manage/qrcode', {
        data: {
          scan_type: 'apply_manage'
        }
      })
        .then((res) => {
          QRCode.toDataURL(res.data.data, {
            margin: 1
          }, (error, url) => {
            if (!error) {
              this.qrcode = url
              this.listenScan()
            }
          })
        })
    },
    /**
     * 监听二维码是否过期&&用户扫码
     */
    listenScan() {
      clearInterval(ScanTimer)
      ScanTimer = setInterval(() => {
        this.$get('manage/test-qrcode', {
          data: {
            scan_type: 'apply_manage'
          }
        })
          .then((res) => {
            const { data, member } = res.data
            if (data) {
              if (member) {
                this.member = member
                this.$emit('change', this.member.id)
                clearInterval(ScanTimer)
              } else {
                this.qrcode_past = false
              }
            } else {
              this.qrcode_past = true
              clearInterval(ScanTimer)
            }
          })
      }, 1000)
    },
    /**
     * 获取管理员头像昵称
     */
    getMember() {
      this.$get('member/' + this.value)
        .then((res) => {
          this.member = res.data.data
        })
    }
  }
}
</script>

<style scoped lang="scss">
  .member-input {
    position: relative;
    width: 130px;
    clear: both;
    line-height: 100%;
    img.qrcode{
      width: 100%;
      height: 130px;
    }
    p.nickname{
      margin: 0;
      padding: 0;
      height: 20px;
      line-height: 20px;
      text-align: center;
    }
    #qrcode-past{
      position: absolute;
      top:0;
      left:0;
      width: 130px;
      height: 130px;
      background: rgba(0,0,0,.7);
      cursor: pointer;
      .qrcode-past{
        text-align: center;
        position: absolute;
        width: 100%;
        top:50%;
        transform: translateY(-50%);
        color: #fff;
      }
    }
    #close{
      position: absolute;
      top:0;
      right: 0;
      cursor: pointer;
    }
  }
</style>
